﻿Imports System
Imports System.Drawing
Imports System.Text.RegularExpressions
Imports System.Windows.Forms
Imports nsShellZipper




Public Class Form1

    Public DIR_OEBPS_ As System.IO.DirectoryInfo

    Dim dir_name As String = "c:\Users\mKane\desktop\tp_epub11_12"
    Public dir_oebps As String = "c:\Users\mKane\desktop\tp_epub11_12\OEBPS"
    Dim dir_meta_inf As String = "c:\Users\mKane\desktop\tp_epub11_12\META-INF"
    Dim dir_images As String = "c:\Users\mKane\desktop\tp_epub11_12\OEBPS\images"

    Dim text_mimetype As String = "application/epub+zip"
    Dim file_mimetype As String = "c:\Users\mKane\Desktop\tp_epub11_12\mimetype"

    Dim file_content_html As String = "c:\Users\mKane\desktop\tp_epub11_12\OEBPS\content.html"
    Dim file_content_opf As String = "c:\Users\mKane\desktop\tp_epub11_12\OEBPS\content.opf"
    Dim file_title As String = "c:\Users\mKane\desktop\tp_epub11_12\OEBPS\title.html"
    Dim file_toc As String = "c:\Users\mKane\desktop\tp_epub11_12\OEBPS\toc.ncx"
    Dim file_stylesheet As String = "C:\Users\mkane\Desktop\WS11-12\Team Projekt\epub\OEBPS\stylesheet.css"
    Dim toFile As String = "c:\Users\mKane\desktop\tp_epub11_12\OEBPS\stylesheet.css"

    Dim file_container As String = "c:\Users\mKane\desktop\tp_epub11_12\META-INF\container.xml"

    Dim fromBild1 As String = "c:\Users\mKane\desktop\foto\madlouise.jpeg"
    Dim toBild1 As String = "c:\Users\mKane\desktop\tp_epub11_12\OEBPS\images\madLouise.jpeg"
    Dim fromBild2 As String = "c:\Users\mKane\desktop\foto\dalaylama.png"
    Dim toBild2 As String = "c:\Users\mKane\desktop\tp_epub11_12\OEBPS\images\dalaylama.png"

    Dim file_extension() As String = {".jpg", ".png", ".bmp", ".gif"}

    Dim file_content_text As String = "this a epub file"
    Dim url_data = """http://www.w3.org/1999/xhtml"""
    Dim xml_header As String = "<html xmlns = " & url_data & ">" & vbCrLf

    Dim file_title_content As String = "    <head>" & vbCrLf & "        <title>Hello World: My First EPUB</title>" &
                                    vbCrLf & "      <link type=" & """text/css""" & " rel=" & """stylesheet""" & " href=" & """stylesheet.css""" & "/>" &
                                    vbCrLf & "  <head>" & vbCrLf & "    <body>" & vbCrLf & "        <h1>Kurlicht einer Scheinehe</h1>" &
                                    vbCrLf & "      <div><img src=" & """images/dalaylama.png""" & "alt=" & """Title page""" & "/></div>" &
                                    vbCrLf & "  </body>" & vbCrLf & "</html>"

    Dim file_content_content As String = "  "


    Public Sub create_Title_file_html(ByVal str As String)

        Dim fsDatei As Object
        Dim fsinhalt As Object

        'html-Datei erstellen
        fsDatei = CreateObject("Scripting.FileSystemObject")
        fsDatei.CreateTextFile("c:\Users\mKane\desktop\tp_epub11_12\OEBPS\title.html")

        fsDatei = fsDatei.getfile("c:\Users\mKane\desktop\tp_epub11_12\OEBPS\title.html")

        fsinhalt = fsDatei.OpenAsTextStream(2, -2)
        fsinhalt.write(str)
        fsinhalt.Close()

    End Sub

    Public Function ReadFileFunc(ByRef filename As String) As String
        Dim textFileStream As New IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
        Dim fileReader As New IO.StreamReader(textFileStream)
        Dim text As String = fileReader.ReadToEnd
        fileReader.Close()
        textFileStream.Close()
        Return text
    End Function

    Public Sub WriteFileFunc(ByVal filename As String, ByVal content As String)
        Dim textFileWriter As New IO.FileStream(filename, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.None)
        Dim fileWriter As New IO.StreamWriter(textFileWriter)
        fileWriter.Write(content)
        fileWriter.Close()
        textFileWriter.Close()
    End Sub

    'convert the image file to bytes()

    Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As Byte()
        Dim _tempByte() As Byte = Nothing
        If String.IsNullOrEmpty(ImageFilePath) = True Then
            Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
            Return Nothing
        End If
        Try
            Dim _fileInfo As New IO.FileInfo(ImageFilePath)
            Dim _NumBytes As Long = _fileInfo.Length
            Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
            Dim _BinaryReader As New IO.BinaryReader(_FStream)
            _tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
            _fileInfo = Nothing
            _NumBytes = 0
            _FStream.Close()
            _FStream.Dispose()
            _BinaryReader.Close()
            Return _tempByte
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    ' Array of Bytes to Image File

    Public Sub ConvertBytesToImageFile(ByVal ImageData As Byte(), ByVal FilePath As String)
        Try
            Dim fs As IO.FileStream = New IO.FileStream(FilePath, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
            Dim bw As IO.BinaryWriter = New IO.BinaryWriter(fs)
            bw.Write(ImageData)
            bw.Flush()
            bw.Close()
            fs.Close()
            bw = Nothing
            fs.Dispose()
        Catch ex As Exception
            ex.InnerException.ToString()
        End Try
    End Sub

    Public Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Dim pfad As String
        Dim openFile As New OpenFileDialog
        openFile.Filter = "jpg Files(*.jpg)* | *.jpg"
        If openFile.ShowDialog() = DialogResult.OK Then
            pfad = openFile.FileName
            PictureBox1.Image = Image.FromFile(pfad)
        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Close()
    End Sub

    'CREATE THE FILE
    '------------------------------------------------------------------------------------------
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        System.IO.Directory.CreateDirectory(dir_name)

        My.Computer.FileSystem.WriteAllText(file_mimetype, text_mimetype, True)

        System.IO.Directory.CreateDirectory(dir_oebps)
        My.Computer.FileSystem.WriteAllText(file_content_html, file_content_text, True)
        My.Computer.FileSystem.WriteAllText(file_content_opf, file_content_text, True)
        'My.Computer.FileSystem.WriteAllText(file_title, xml_header, True)
        ' My.Computer.FileSystem.WriteAllText(file_title, file_title_content, True)
        '     My.Computer.FileSystem.WriteAllText(file_title, main_content, True)
        My.Computer.FileSystem.WriteAllText(file_toc, file_content_text, True)
        'My.Computer.FileSystem.WriteAllText(toFile, "bal bla bla", True)

        create_Title_file_html(file_title_content)

        Dim inhalt As String = ReadFileFunc(file_stylesheet)
        WriteFileFunc(toFile, inhalt)
        MsgBox(inhalt)

        System.IO.Directory.CreateDirectory(dir_meta_inf)
        My.Computer.FileSystem.WriteAllText(file_container, file_content_text, True)

        System.IO.Directory.CreateDirectory(dir_images)
        Dim image_data1() As Byte = ConvertImageFiletoBytes(fromBild1)
        ConvertBytesToImageFile(image_data1, toBild1)

        Dim image_data2() As Byte = ConvertImageFiletoBytes(fromBild2)
        ConvertBytesToImageFile(image_data2, toBild2)

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim file_name As String = "c:\Users\mKane\desktop\tp_epub11_12\mimetype"
        Dim text_mimetype As String = "application/epub+zip"
        'Dim i As Integer
        '       Dim aryText(4) As String
        '       aryText(0) = "Gnagna je t'aime"

        If System.IO.File.Exists(file_name) = True Then
            Dim ObjWriter As New System.IO.StreamWriter(file_name)
            '         For i = 0 To 4
            'ObjWriter.WriteLine(aryText(i))
            '      Next
            ObjWriter.Write(text_mimetype)
            ObjWriter.Close()
        Else
            MsgBox("File doesn't exists")
        End If

    End Sub

    Private Sub Button6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim dir_name As String = "c:\Users\mKane\desktop\tp_epub11_12"
        System.IO.Directory.CreateDirectory(dir_name)

    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim toZip = "C:\Users\mkane\Desktop\tp_epub11_12"
        Using shell = New ShellZipper
            shell.Zip(toZip)
            MsgBox("Zip done")
        End Using


    End Sub
End Class
